package io.xccit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.model.system.SysUser;
import io.xccit.model.vo.RouterVo;
import io.xccit.model.vo.SysUserQueryVo;
import io.xccit.system.mapper.ISysUserMapper;
import io.xccit.system.service.ISysMenuService;
import io.xccit.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CH_ywx
 * @date 2023-06-22
 * @description
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<ISysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private ISysUserMapper sysUserMapper;

    @Resource
    private ISysMenuService sysMenuService;
    @Override
    public List<SysUser> selectList() {
        return sysUserMapper.selectList(null);
    }

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> sysUserPage, SysUserQueryVo sysUserQueryVo) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysUserQueryVo.getKeyword())){
            wrapper.like(SysUser::getUsername,sysUserQueryVo.getKeyword())
                    .or()
                    .like(SysUser::getName,sysUserQueryVo.getKeyword());
        }
        return sysUserMapper.selectPage(sysUserPage,wrapper);
    }

    /**
     * 添加用户时保证用户名唯一
     * @param username 用户名
     * @return 是否存在
     */
    @Override
    public boolean userNameIsExist(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        if (sysUser != null){
            return false;
        }
        return true;
    }

    @Override
    public SysUser login(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        //获取菜单列表
        List<RouterVo> routerVoList = sysMenuService.getUserMenuList(sysUser.getId());
        //获取按钮权限
        List<String> permList = sysMenuService.getUserButtonList(sysUser.getId());
        Map<String,Object> result = new HashMap<>();
        result.put("name",sysUser.getName());
        result.put("avatar",sysUser.getHeadUrl());
        result.put("roles",sysUser.getRoleList());
        result.put("routers",routerVoList);
        result.put("buttons",permList);
        return result;
    }

    @Override
    public SysUser getUserInfoByUserName(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        return sysUserMapper.selectOne(wrapper);
    }
}
